package com.eova.repair.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.alipay.util.ShowImg;
import com.eova.repair.model.RBanner;
import com.eova.repair.model.RBrand;
import com.eova.repair.model.RModel;
import com.eova.repair.model.ROrder;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.ext.render.CaptchaRender;

/**
 *前台controller
 *  2015年11月19日
 */


public class FrontController extends Controller {
	
	final Controller ctrl = this;
	
	private String order_vcode="ORDERCODEKEY";
	
	public void index()
	{
		List<RBrand> brandList = RBrand.dao.getBrandList();	
		String chars = "abcdefghijklmnopqrstuvwxyz";
		ctrl.setAttr("brandList", brandList);
		List<RBanner> bannerList = RBanner.dao.getBannerList();	
		ctrl.setAttr("bannerList", bannerList);

		String a=chars.charAt((int)(Math.random() * 26))+"";
		String b=chars.charAt((int)(Math.random() * 26))+"";
		Date date=new Date();
		setAttr("order_no",a+b+date.getTime());
		render("/repair/index.html");
	}
	
	/**
	 * 生成验证码
	 */
	@Clear
	public void vcodeImg() {
		render(new CaptchaRender(order_vcode));
	}

	@Clear
	public void getModelJson(){
		renderJson(RModel.dao.find("select * from r_model t where t.r_brand_id=? ",getPara(0)));
	}
	
	@Clear
	public void getRepairJson()
	{
		renderJson(RModel.dao.find("select * from r_repair_type t where  t.r_model_id=? ",getPara(0)));
	}

	@Clear
	public void to_order()
	{
		if(CaptchaRender.validate(this, getPara("wfcode").toUpperCase(), order_vcode)){
			String chars = "abcdefghijklmnopqrstuvwxyz";
			String a=chars.charAt((int)(Math.random() * 26))+"";
			String b=chars.charAt((int)(Math.random() * 26))+"";
			Date date=new Date();
			String order_no= a+b+date.getTime();
			ROrder rOrder=new ROrder();
			String brandStr=getPara("brand_id");
			String modelStr=getPara("model_id");
			String[] repairTypeStrs=getParaValues("repair_type_id");
			
			String[] brands=brandStr.split("_");
			String[] models=modelStr.split("_");
			
			String repair_type_id="";String repair_type_name="";
			for(String repairTypeStr:repairTypeStrs){
				String[] repairTypes=repairTypeStr.split("_");
				repair_type_id+=repairTypes[0]+",";
				repair_type_name+=repairTypes[1]+",";
			}
			rOrder.set("order_no",order_no);
			rOrder.set("brand_id", brands[0]);
			rOrder.set("brand_name", brands[1]);
			rOrder.set("model_id",models[0]);
			rOrder.set("model_name",models[1]);
			rOrder.set("repair_type_id",repair_type_id);
			rOrder.set("repair_type_name",repair_type_name);
			rOrder.set("repair_fees",getPara("repair_fees"));
			rOrder.set("mobile_no",getPara("mobile_no"));
			rOrder.set("area_name",getPara("wfprovince")+getPara("wfcity")+getPara("wfarea"));
			rOrder.set("pay_type", getPara("pay_type"));
			rOrder.set("words_message", getPara("words_message"));
			rOrder.set("order_time",date);
			rOrder.set("is_pay",0);	
			rOrder.save();
			
			rOrder= ROrder.dao.find("select * from r_order t where  t.order_no=? ",order_no).get(0);
			setAttr("rOrder",rOrder);
			renderJsp("/index.jsp");
		}else{
			setAttr("error_info","您的验证码有误");
			index();
		};
		
	}
	
	@Clear
	public void vcodeImg2() {		
		ShowImg.showImg(new File(JFinal.me().getServletContext().getRealPath("")));
	}
	
}
